#ifndef CALCULATOR_H
#define CALCULATOR_H

#include <string>
#include <complex>
#include <functional>

// 复数类型定义
using Complex = std::complex<double>;

class Calculator {
private:
    // 表达式解析相关
    std::string expression;
    size_t pos;

    // 解析表达式
    double parseFactorial();
    double parseAngleConversion();
    double parseExpression();
    double parseTerm();
    double parseFactor();
    double parseFunction();
    double parseNumber();

    // 工具函数
    void skipWhitespace();
    bool isOperator(char c);
    bool isFunction(const std::string &name);

    // 科学计算函数
    static double factorial(int n);
    static double degToRad(double deg);
    static double radToDeg(double rad);

    // 数值计算方法
    static double numericalDerivative(const std::function<double(double)> &f, double x, double h = 1e-7);
    static double numericalIntegral(const std::function<double(double)> &f, double a, double b, int n = 10000);

public:
    Calculator();

    // 基本计算接口
    double evaluate(const std::string &expr);

    // 复数运算接口
    Complex complexAdd(const Complex &a, const Complex &b);
    Complex complexSubtract(const Complex &a, const Complex &b);
    Complex complexMultiply(const Complex &a, const Complex &b);
    Complex complexDivide(const Complex &a, const Complex &b);
    Complex complexPower(const Complex &base, const Complex &exp);
    Complex complexSqrt(const Complex &z);
    Complex complexSin(const Complex &z);
    Complex complexCos(const Complex &z);
    Complex complexTan(const Complex &z);
    Complex complexExp(const Complex &z);
    Complex complexLog(const Complex &z);

    // 微积分接口
    double derivative(const std::function<double(double)> &f, double x);
    double integral(const std::function<double(double)> &f, double a, double b);

    // 预定义函数
    static double functionSin(double x);
    static double functionCos(double x);
    static double functionTan(double x);
    static double functionAsin(double x);
    static double functionAcos(double x);
    static double functionAtan(double x);
    static double functionSinh(double x);
    static double functionCosh(double x);
    static double functionTanh(double x);
    static double functionLn(double x);
    static double functionLog10(double x);
    static double functionSqrt(double x);
    static double functionExp(double x);
    static double functionAbs(double x);
    static double functionFloor(double x);
    static double functionCeil(double x);
};

#endif // CALCULATOR_H
